package com.youzikj.wx.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.youzikj.wx.comm.pojo.Sign;
import com.youzikj.wx.comm.util.CommonUtil;
import com.youzikj.wx.comm.util.SignUtil;
import com.youzikj.wx.comm.util.Utils;
import com.youzikj.wx.service.MsgCoreService;

/**
 * 
 * 微信公众号验证接口
 */
@RestController
@RequestMapping(value = "/weixin/interface/")
public class ValidateController {

	private static final Logger log = Logger.getLogger(ValidateController.class);
	
	public String Token = "weiyouyouzikj";

	@RequestMapping(value = "validate")
	public String validate(HttpServletRequest request,
			HttpServletResponse response) {
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");
		String echostr = request.getParameter("echostr");
		String signature = request.getParameter("signature");

		if (!Utils.hasValue(signature, timestamp, nonce)) {
			log.error("wx token validate fail : params is error.");
			return "fail";
		} else if (null != echostr && !echostr.equals("")) {
			// 请求检验，若检验成功则原样返回echostr,表示接入成功，否则失败
			if (SignUtil.checkSignature(signature, timestamp, nonce)) {
				log.info("wx token validate success.");
				return echostr;
			}
			log.error("wx token validate fail : signature is not equal.");
			return "fail";
		}
		/***************** 消息处理 *****************************/
		log.info("wx server out request.");
		// 请求校验
		if (SignUtil.checkSignature(signature, timestamp, nonce)) {
			log.info("wx request token validate success.");
			// 调用核心服务类接收处理请求
			String respXml = MsgCoreService.processRequest(request);
			log.info("respXml响应："+respXml);
			return respXml;
		}
		return "fail";
	}

	
	/**
	 * 获取关注者用户信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="subInfo")
	public void getsubInfo(HttpServletRequest request){
		
	}
	
	
	@RequestMapping(value = "redirect")
	public String redirect(HttpServletRequest request){
		//查询要跳转的地址
		System.out.println("r3435315324");
		 
		//设置授权网页链接
		//生成二维码
		return "1";
	}
	
	
	
	

	
	

}
